<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;

class LoginController extends Controller
{
    //登陆页面
    public function index(){
        return view('login.index');
    }
    //登陆行为
    public function login(){
        //1.验证
        $this->validate(request(),[
            'email'=>'required|email',
            'password'=>'required|min:5',
            'is_remember'=>'integer'
        ]);
        //2.逻辑
        $user = request(['email','password']);
        $is_remember = boolval(request('is_remember'));
        //3.渲染
        if (Auth::attempt($user,$is_remember)){
            //如果认证成功
            return redirect('/posts');
        }
        //失败
        return redirect()->back()->withErrors('登陆失败,邮箱密码不匹配');
    }
    //登出行为
    public function logout(){
        Auth::logout();
        return redirect('/login');
    }
}
